//
//  AmrRecorder.m
//  aaaa
//
//  Created by DouLei on on 2017/7/17.
//  Copyright © 2017年 Andrew. All rights reserved.
//

#import "ST_DPAudioRecorder.h"

#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
#import "ST_JX_GCDTimerManager.h"


#define MAX_RECORDER_TIME 60
#define MIN_RECORDER_TIME 1

#define TimerName @"audioTimer_999"

@interface ST_DPAudioRecorder () <AVAudioRecorderDelegate>
{
    BOOL isRecording;
    dispatch_source_t timer;
    NSUInteger __block audioTimeLength; //录音时长
}

@property (nonatomic, strong) AVAudioRecorder *audioRecorder;

@end

@implementation ST_DPAudioRecorder

static ST_DPAudioRecorder *recorderManager = nil;
+ (ST_DPAudioRecorder *)sharedInstance
{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken,^{
        recorderManager = [[ST_DPAudioRecorder alloc] init];
    });
    
    return recorderManager;
}

- (instancetype)init
{
    if (self = [super init]) {
        //创建缓存录音文件到Tmp
        NSString *wavRecordFilePath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"WAVtemporaryRadio.wav"];
       

        if (![[NSFileManager defaultManager]fileExistsAtPath:wavRecordFilePath]) {
            [[NSData data] writeToFile:wavRecordFilePath atomically:YES];
        }
        
    }
    return self;
}

- (void)startRecording
{
    if (isRecording) return;
    
    
    //开始录音
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];

    [self.audioRecorder prepareToRecord];
    
    [self.audioRecorder record];
    
    if ([self.audioRecorder isRecording]) {
        isRecording = YES;
        [self activeTimer];
        if (self.audioStartRecording) {
            self.audioStartRecording(YES);
        }
    } else {
        if (self.audioStartRecording) {
            self.audioStartRecording(NO);
        }
    }
    
    
    [self createPickSpeakPowerTimer];
}

- (void)stop_recording;
{
    if (!isRecording) return;
    
    [self shutDownTimer];
    [self.audioRecorder stop];
    self.audioRecorder = nil;
}

- (void)activeTimer
{
    //录音时长
    audioTimeLength = 0;
    
    [[ST_JX_GCDTimerManager sharedInstance]scheduledDispatchTimerWithName:TimerName timeInterval:0.5 queue:nil repeats:YES actionOption:AbandonPreviousAction action:^{
        self->audioTimeLength ++;
        if (self->audioTimeLength >= 60*2) { //大于等于60秒停止
            [self stop_recording];
        }
    }];
}

- (void)shutDownTimer
{
    [[ST_JX_GCDTimerManager sharedInstance] cancelAllTimer];//定时器停止
}

- (AVAudioRecorder *)audioRecorder
{
    if (!_audioRecorder) {
        
        //暂存录音文件路径
        NSString *wavRecordFilePath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"WAVtemporaryRadio.wav"];
        
        NSDictionary *recordSetting = @{AVFormatIDKey: @(kAudioFormatLinearPCM),
                                   AVSampleRateKey: @8000.00f,
                                   AVNumberOfChannelsKey: @1,
                                   AVLinearPCMBitDepthKey: @16,
                                   AVLinearPCMIsNonInterleaved: @NO,
                                   AVLinearPCMIsFloatKey: @NO,
                                   AVLinearPCMIsBigEndianKey: @NO};
        
        _audioRecorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:wavRecordFilePath] settings:recordSetting error:nil];
        _audioRecorder.delegate = self;
        _audioRecorder.meteringEnabled = YES;
    }
    return _audioRecorder;
}

#pragma mark - AVAudioRecorder

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
    //暂存录音文件路径
    NSString *wavRecordFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"WAVtemporaryRadio.wav"];
    
    
    //返回wav音频文件Data,用于传输或存储
    NSData *cacheAudioData = [NSData dataWithContentsOfFile:wavRecordFilePath];
    
    //大于最小录音时长时,发送数据
    if (audioTimeLength > MIN_RECORDER_TIME) {
        if (self.audioRecorderFinishRecording) {
            self.audioRecorderFinishRecording(cacheAudioData, audioTimeLength);
        }
    } else {
        if (self.audioRecordingFail) {
            self.audioRecordingFail(@"说话时间太短");
        }
    }
    
    isRecording = NO;
    
    //取消定时器
    if (timer) {
        dispatch_source_cancel(timer);
        timer = NULL;
    }
    
}

//音频值测量
- (void)createPickSpeakPowerTimer
{
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC, 1ull * NSEC_PER_SEC);
    
    __weak __typeof(self) weakSelf = self;
    
    dispatch_source_set_event_handler(timer, ^{
        __strong __typeof(weakSelf) _self = weakSelf;
        
        [_self->_audioRecorder updateMeters];
        double lowPassResults = pow(10, (0.05 * [_self->_audioRecorder peakPowerForChannel:0]));
        if (_self.audioSpeakPower) {
            _self.audioSpeakPower(lowPassResults);
        }
    });
    
    dispatch_resume(timer);
}

- (void)dealloc
{
    if (isRecording) [self.audioRecorder stop];
     [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
